<?php

namespace yiike\company\backend\controllers;
use yiike\company\model\TtycTokerComboOrder;
use yiike\company\model\TtycTokerComboOrderSearch;
use dmstr\bootstrap\Tabs;
use yii\web\HttpException;
use yii\helpers\Url;
use yii\filters\AccessControl;
/**
* This is the class for controller "TtycTokerComboOrderController".
*/
class TtycTokerComboOrderController extends \yiike\company\backend\controllers\base\TtycTokerComboOrderController
{
    public function actionIndex()
    {
        $_GET['TtycTokerComboOrderSearch']['company_id'] = \Yii::$app->user->identity->id;
        $searchModel  = new TtycTokerComboOrderSearch;
        $dataProvider = $searchModel->search($_GET);

        Tabs::clearLocalStorage();

        Url::remember();
        \Yii::$app->session['__crudReturnUrl'] = null;

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }
    public function actionExport()
    {
        $model = new TtycTokerComboOrder;
        $data = $model->find()->where(['company_id'=>\Yii::$app->user->identity->id])->asArray()->all();



        \moonland\phpexcel\Excel::export([
            'models' => $data,
            'fileName'=>date('YmdHis',time()).'xls',
            'columns' => [
                [
                    'attribute' => 'trade_id',
                    'header' => '订单编号',
                    'format' => 'text',
                    'value' => function($model) {
                            return $model['trade_id'];
                    },
                ],
                [
                    'attribute' => 'name',
                    'header' => '收件人',
                    'format' => 'text',
                    'value' => function ($model) {
                        return $model['name'];
                    }
                ],
                [
                    'attribute' => 'mobile',
                    'header' => '手机',
                    'format' => 'text',
                    'value' => function ($model) {
                        return $model['mobile'];
                    }
                ],
                [
                    'attribute' => 'address',
                    'header' => '地址',
                    'format' => 'text',
                    'value' => function ($model) {
                        return $model['address'];
                    }
                ],
            [
                'attribute' => 'info',
                'header' => '发货信息',
                'format' => 'text',
                'value' => function ($model) {
                    return $model['title'];
                }
            ],
            ],
            'headers' => [
                'created_at' => 'Date Created Content',
            ],
        ]);

    }
    //订单发货
    public function actionUpdate($id)
    {
        $connection  = \Yii::$app->db;
        $sql     = "select id,name,code from ims_ttyc_shop_express where status='y' ";
        $command = $connection->createCommand($sql);
        $res     = $command->queryAll();
        $model = $this->findModel($id);
        if ($model->load($_POST) ) {
            $express_id = $_POST['TtycTokerComboOrder']['express_company'];
            $sql     = "select code from ims_ttyc_shop_express where id= $express_id  ";
            $command = $connection->createCommand($sql);
            $code     = $command->queryOne()['code'];
            $model->express_company = $code;
            $model->status=2;
            if($model->save()){
                return $this->redirect(Url::previous());
            }
        } else {
            return $this->render('update', [
                'model' => $model,
                'data'=>$res
            ]);
        }
    }
}
